package com.woniuxy.stage3demo.mapper.chenyuxuanMapper;


import com.woniuxy.stage3demo.bean.chenyuxuanBean.EntranceRuleBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * 准入规则mapper类
 */
@Mapper
public interface EntranceRuleMapper {

    /**
     * 添加准入规则
     *
     * @param entranceRuleBean
     * @return
     */
    @Options(useGeneratedKeys = true,
            keyProperty = "e.id")
    @Insert("insert into entrance_rule (no,rule_name,rule_desc,start_time,end_time,use_status,user_num,del_flag)" +
            "values(#{e.no},#{e.ruleName},#{e.ruleDesc},#{e.startTime},#{e.endTime},#{e.useStatus},#{e.userNum},#{e.delFlag})")
    int addEntranceRule(@Param("e") EntranceRuleBean entranceRuleBean);

    /**
     * 删除准入规则
     *
     * @param id
     */
    @Update("update entrance_rule set del_flag = 1 where id = #{id}")
    int deleteEntranceRule(@Param("id") int id);

    /**
     * 修改准入规则
     *
     * @param entranceRuleBean
     */
    @Update("<script>" +
            "update entrance_rule" +
            "<set>" +
            "<if test='e.no != null'>" +
            " no = #{e.no}," +
            "</if>" +
            "<if test='e.ruleName != null'>" +
            " rule_name = #{e.ruleName}," +
            "</if>" +
            "<if test='e.ruleDesc!= null'>" +
            " rule_desc = #{e.ruleDesc}," +
            "</if>" +
            "<if test='e.startTime != null'>" +
            " start_time = #{e.startTime}," +
            "</if>" +
            "<if test='e.endTime != null'>" +
            " end_time = #{e.endTime}," +
            "</if>" +
            "<if test='e.useStatus != null'>" +
            " use_status = #{e.useStatus }," +
            "</if>" +
            "<if test='e.userNum != null'>" +
            " user_num = #{e.userNum }," +
            "</if>" +
            "<if test='e.delFlag != null'>" +
            " del_flag = #{e.delFlag }," +
            "</if>" +
            "</set>" +
            "where id = #{e.id}" +
            "</script>")
    int updateEntranceRule(@Param("e") EntranceRuleBean entranceRuleBean);

    /**
     * 根据内容模糊查询准入规则
     *
     * @param entranceRuleBean
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = int.class),
            @Result(property = "no", column = "no", javaType = String.class),
            @Result(property = "ruleName", column = "rule_name", javaType = String.class),
            @Result(property = "ruleDesc", column = "ruleDesc", javaType = String.class),
            @Result(property = "startTime", column = "start_time", javaType = Date.class),
            @Result(property = "endTime", column = "end_time", javaType = Date.class),
            @Result(property = "useStatus", column = "use_status", javaType = int.class),
            @Result(property = "userNum", column = "user_num", javaType = String.class),
    })
    @Select("<script>" +
            "select * from entrance_rule " +
            "<where> " +
            "del_flag=0 " +
            "<if test='e.id!=null'>" +
            "and id = #{e.id} " +
            "</if> " +
            "<if test='e.ruleName!=null'>" +
            "and rule_name like concat('%',#{e.ruleName},'%')" +
            "</if>" +
            "</where>" +
            "</script>")
    EntranceRuleBean findEntranceRuleByContext(@Param("e") EntranceRuleBean entranceRuleBean);

    /**
     * 分页查询所有准入规则
     *
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = int.class),
            @Result(property = "no", column = "no", javaType = String.class),
            @Result(property = "ruleName", column = "rule_name", javaType = String.class),
            @Result(property = "ruleDesc", column = "rule_desc", javaType = String.class),
            @Result(property = "startTime", column = "start_time", javaType = Date.class),
            @Result(property = "endTime", column = "end_time", javaType = Date.class),
            @Result(property = "useStatus", column = "use_status", javaType = int.class),
            @Result(property = "userNum", column = "user_num", javaType = Long.class),
            @Result(property = "delFlag", column = "del_flag", javaType = String.class)
    })
    @Select("<script>" +
            "select id,no,rule_name,rule_desc,start_time,end_time,use_status,user_num from entrance_rule " +
            "<where> " +
            "del_flag=0 " +
            "<if test='e.id!=null and e.id!=0'> " +
            "and id = #{e.id} " +
            "</if> " +
            "<if test='e.ruleName!=null'>" +
            "and rule_name like concat('%',#{e.ruleName},'%') " +
            "</if>" +
            "<if test='p.sort != null and p.sortOrder != null'>" +
            "order by ${p.sort} ${p.sortOrder}" +
            "</if> " +
            "</where>" +
            "</script>")
    List<EntranceRuleBean> findAllEntranceRule(@Param("p") PageRequest pageRequest, @Param("e") EntranceRuleBean entranceRuleBean);
}
